package com.ebupt.migu.music.autoTri.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.music.autoTri.Entity.MarketingBean;
import com.ebupt.migu.music.autoTri.Entity.ResultBean;
import com.ebupt.migu.music.autoTri.Entity.SendResult;
import com.ebupt.migu.music.common.exception.MiguRuntimeException;
import com.ebupt.migu.music.common.util.HttpUtils;
import com.ebupt.migu.music.common.util.HttpsUtils;
import com.ebupt.migu.music.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xuhongsong
 * @version 1.0
 * @date 2020/7/27 16:06
 */
@Slf4j
public class SongSender implements Sender {
    //  门户查询统一CMS歌曲的接口
    @Override
    public List<ResultBean> Send(Map<String, Object> paramsMap) {
        //  第一次执行的
        SendResult sendResult = song(paramsMap);
        List<ResultBean> list = sendResult.getResultBeanList();
        Boolean IsHave = false;
        switch (sendResult.getIsEnd()) {
            case "true":
                IsHave = true;
                break;
            case "false":
                IsHave = false;
                break;
        }
        int code = 1;
        while (IsHave) {
            paramsMap.put("startQueryId", sendResult.getStartQueryId());
            sendResult = song(paramsMap);
            code++;
            list.addAll(sendResult.getResultBeanList());
        }
        log.info("统一CMS歌曲数据次数:{}" + code);
        return list;
    }

    // 门户查询统一CMS歌曲的接口
    private SendResult song(Map<String, Object> paramsMap) {
        log.info("调用门户查询统一CMS歌曲的接口, catalogRequest:{}", paramsMap.toString());
        String result;
        try {
            result = HttpUtils.doGetRequest(paramsMap.get("url").toString(), paramsMap);
        } catch (Exception e) {
            log.error("调用CMS歌曲接口失败", e);
            throw new MiguRuntimeException("调用CMS歌曲接口失败", e);
        }
        log.info("CMS歌曲实体返回:{}", result);
        //  返回map
        Map songMap = JsonUtils.json2Obj(result, Map.class);
        if (!"000000".equals(songMap.get("state"))) {
            log.error("获取CMS歌曲数据失败");
            throw new MiguRuntimeException("获取CMS歌曲数据失败" + songMap.get("message"));
        }
        Map contentMap = (Map) songMap.get("content");
        // 所有更新数据是否已经完全查询  true or false
        String isEnd = contentMap.get("isEnd").toString();
        // 最新的时间戳Id
        String lastChangeId = contentMap.get("lastChangeId").toString();
        JSONArray contentList = (JSONArray) contentMap.get("list");
        if (contentList == null || contentList.isEmpty()) {
            log.error("获取CMS歌曲实体接口数据为空");
        }
        List<Map> ObjectList = JSONObject.parseArray(contentList.toJSONString(), Map.class);
        //  构造结果集
        List<ResultBean> list = ObjectList.stream().map(
                e -> new ResultBean(Integer.valueOf(e.get("songId").toString()),
                        e.get("songName").toString(), e.get("songCreateTime").toString(),
                        e.get("songUpdateTime").toString(), 2, "")
        ).collect(Collectors.toList());
        return new SendResult(isEnd, lastChangeId, list);
    }


}
